{% extends "chat_base.html" %}

{% block "title" %}using subscribe-broadcast{% endblock %}

{% block introduction %}
<p>Point a second browser onto this URL and check for message synchronization.</p>
{% endblock introduction %}

{% block script_panel %}
<script type="text/javascript">
jQuery(document).ready(function($) {
	var ws4redis = WS4Redis({
		uri: '{{ WEBSOCKET_URI }}foobar?subscribe-broadcast&publish-broadcast&echo',
		receive_message: receiveMessage,
		heartbeat_msg: {{ WS4REDIS_HEARTBEAT }}
	});
	var billboard = $('#billboard');

	// send message though the Websocket to the server
	$("#text_message").keydown(function(event) {
		if (event.keyCode === 13) {
			event.preventDefault();
			ws4redis.send_message($('#text_message').val());
		}
	});

	$('#send_message').click(function() {
		ws4redis.send_message($('#text_message').val());
	});

	// receive a message though the Websocket from the server
	function receiveMessage(msg) {
		billboard.append('<br/>' + msg);
		billboard.scrollTop(billboard.scrollTop() + 25);
	}
});
</script>
{% endblock %}
